<template>
  <div class="container">
    <h2 style="text-align: center">修改密码</h2>
    <el-form
      :model="modifyForm"
      status-icon
      :rules="rules"
      ref="modifyForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="原始密码" prop="oldPwd">
        <el-input type="password" v-model="modifyForm.oldPwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input type="password" v-model="modifyForm.newPwd"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input type="password" v-model="modifyForm.confirmPassword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('modifyForm')">提交</el-button>
        <el-button @click="resetForm('modifyForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { modifyPwd } from "@/api/user";
export default {
  data() {
    var checkPwd = (value) => {
      let reg = /^(\w){6,20}$/;
      return reg.test(value);
    };
    var checkOld = (rule, value, callback) => {
      console.log(value);
      if (value === "") {
        callback(new Error("请输入密码"));
        return;
      }
      if (!checkPwd(value)) {
        callback(new Error("密码由6-20位字母数字下划线组成"));
        return;
      }
      callback();
    };

    var validatePass = (rule, value, callback) => {
      console.log(value);
      if (value === "") {
        callback(new Error("请输入密码"));
        return;
      }
      if (!checkPwd(value)) {
        callback(new Error("密码由6-20位字母数字下划线组成"));
        return;
      }

      if (this.modifyForm.confirmPassword !== "") {
        this.$refs.modifyForm.validateField("confirmPassword");
      }
      callback();
    };
    var validatePass2 = (rule, value, callback) => {
      console.log(value);
      if (value === "") {
        callback(new Error("请再次输入密码"));
        return;
      } else if (value !== this.modifyForm.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      modifyForm: {
        oldPwd: "",
        newPwd: "",
        confirmPassword: "",
      },
      rules: {
        oldPwd: [{ validator: checkOld, trigger: "blur" }],
        newPwd: [{ validator: validatePass, trigger: "blur" }],
        confirmPassword: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    // modify() {
    //   this.$refs["modifyForm"].validate((valid) => {
    //     if (valid) {
    //       alert("submit!");
    //     } else {
    //       console.log("error submit!!");
    //       return false;
    //     }
    //   });
    // },
    resetForm(modifyForm) {
      this.$refs["modifyForm"].resetFields();
    },
    submitForm(val) {
      this.$refs["modifyForm"].validate((valid) => {
        if (valid) {
          modifyPwd(this.modifyForm).then((res) => {
            // res = res.data;
            if (res.code !== 200) {
              return this.$message.error(res.message);
            }
            window.sessionStorage.clear();
            ElMessage.success(res.message);
            this.$router.push("/login");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
